package web.xyl;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;

import java.lang.reflect.Field;

public class AppTest {
    public static void main(String[] args) throws Exception {
        // 获取类对象
        Class<?> c = Animal.class;
        // 构造实例
        Object o = c.newInstance();
        // 反射所有的字段
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Class<?> type = field.getType();
            // 通过类对象的 isAssignableFrom 方法来判断其类型
            if (type.isAssignableFrom(String.class)) {
                String s = RandomStringUtils.randomAlphabetic(6);
                field.set(o, s);
            } else if (type.isAssignableFrom(Double.class)) {
                Double v = RandomUtils.nextDouble();
                field.set(o, v);
            }
        }
        System.out.println(o);
    }

}
